import wepy from 'wepy'

export default class baseMixin extends wepy.mixin {
  /**
   * [公共方法]
   * @param  {[type]}  item [description]
   * @return {Boolean}      [description]
   */
  noop() {
    return null;
  }
  hasOwn(obj, type) {
    return Object.prototype.hasOwnProperty.call(obj, type);
  }

  /**
   * [isXXX 基础方法]
   * @param  {[type]}  item [description]
   * @return {Boolean}      [description]
   */
  isUndefined(item) {
    return typeof item === 'undefined';
  }
  isDefined(item) {
    return !this.isUndefined(item);
  }
  isString(item) {
    return typeof item === 'string';
  }
  isNumber(item) {
    return typeof item === 'number';
  }
  isArray(item) {
    return Object.prototype.toString.apply(item) === '[object Array]';
  }
  isObject(item) {
    return typeof item === 'object' && !this.isArray(item);
  }
  isFunction(item) {
    return typeof item === 'function';
  }

  /**
   * [getXXX 增强方法]
   * @param  {[type]}  item [description]
   * @return {Boolean}      [description]
   */
  getString(item, defaultStr) {
    if (this.isString(item)) return item.trim();
    if (this.isNumber(item)) return `${item}`.trim();
    return defaultStr || '';
  }
  getNumber(item, defaultNum) {
    var matches = this.getString(item).match(/\d+/);
    return this.isNumber(matches && +matches[0]) ? +matches[0] : defaultNum;
  }
  getArray(item, defaultArr) {
    return this.isArray(item) ? item : (defaultArr || []);
  }
  getObject(item, defaultObj) {
    return this.isObject(item) ? item : (defaultObj || {});
  }
  getFunction(item) {
    return this.isFunction(item) ? item : noop;
  }

  /**
   * [JSON方法]
   * @param  {[type]}  item [description]
   * @return {Boolean}      [description]
   */
  $json(item) {
    let str = {type: Object.prototype.toString.call(item)}
    try {
      str = JSON.stringify(item)
    } catch (e) {
      str.error = e && e.stack || ''
    }
    return this.isString(str) ? str : this.$json(str)
  }
  $parse(item) {
    let obj = {type: Object.prototype.toString.call(item)}
    try {
      obj = JSON.parse(item)
    } catch (e) {
      obj.error = e && e.stack || ''
    }
    return this.isObject(obj) ? obj : this.$parse(obj)
  }

  /**
   * [功能方法]
   * @param  {[type]}  item [description]
   * @return {Boolean}      [description]
   */
  isPhone(str) {
    return /^1\d{10}$/.test(str)
  }
  $alert(item = '标题', item2) {
    const param = this.isObject(item) ? Object.assign({
      // 首参数为obj
      title: 'title', content: 'content'
    }, item) : this.isString(item) ? this.isString(item2) ? {
      // 俩参数均为字符串
      title: item, content: item2
    } : {
      // 只有首参为字符串
      title: '', content: item
    } : {
      // 尝试转换字符串
      title: item.toString ? item.toString() : '参数异常'
    }
    wx.showModal(Object.assign({
      showCancel: false
    }, param))
  }

  /**
   * [判断是否有网络]
   * @param  {[type]}  item [description]
   * @return string
   */
  getNetworkStatus(callback) {
    wx.getNetworkType({
      success (res) {
        const networkType = res.networkType;
        return typeof callback === 'function' && callback(networkType)
      }
    })
  }

  // 移除数组某个值
  removeByValue(arr, val) {
    for(var i = 0; i < arr.length; i++) {
      if(arr[i] == val) {
        return arr.splice(i, 1);
      }
   }
 }
 // 数组里是否有某个值 返回Boolean
 isArrayHasSome(arr, val) {
   return arr.some(function(item, index, array){
       return item == val;
   });
 }

  // 获取是否已经授权
  getSetting(scope,opts={}) {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success({
          authSetting
        }) {
          resolve(authSetting[`scope.${scope}`])
          opts.success && opts.success(authSetting[`scope.${scope}`])
        },
        fail(err){
          reject(err)
          opts.fail && opts.fail(err)
        }
      })
    })
  }

  //打开设置授权页面
  openSetting(scope, opts={}) {
    return new Promise((resolve, reject) => {
      wx.openSetting({
        success({
          authSetting
        }) {
          if (scope) {
            resolve(authSetting[`scope.${scope}`])
            opts.success && opts.success(authSetting[`scope.${scope}`])
          } else {
            resolve(authSetting)
            opts.success && opts.successopts.success(authSetting)
          }
        },
        fail(err){
          reject(err)
          opts.fail && opts.fail(err)
        }
      })
    })
  }

  //获取用户信息
  getUserInfo(opts={}) {
    return new Promise((resolve, reject) => {
      wx.getUserInfo({
        success(data){
          opts.success && opts.success(data)
        },
        fail(err){
          reject(err)
          opts.fail && opts.fail(err)
        }
      })
    })
  }

  //补零
  zeroize(num){
    return num<10?'0'+num:num;
  }

  //获取dom节点信息
  getRect(selector, all) {
    return new Promise(function (resolve) {
      wx.createSelectorQuery()[all ? 'selectAll' : 'select'](selector).boundingClientRect(function (rect) {
        if (all && Array.isArray(rect) && rect.length) {
          resolve(rect);
        }

        if (!all && rect) {
          resolve(rect);
        }
      }).exec();
    });
  }

  //微信登录拿到code
  login(){
    return new Promise((resolve, reject) => {
      wx.login({
        success(res) {
          resolve(res)
        },
        fail(err){
          reject(err)
        }
      })
    })
  }

  //获取当前的地理位置
  getLocation(){
    return new Promise((resolve, reject) => {
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          resolve(res);
        },
        fail(err){
          reject(err)
        }
      })
    })
  }

  //使用微信内置地图查看位置
  openLocation(opts){
    return new Promise((resolve, reject) => {
      wx.openLocation({
        success(res) {
          resolve(res);
        },
        fail(err){
          reject(err)
        }
      })
    })
  }

  //打开地图选择位置
  chooseLocation(){
    return new Promise((resolve, reject) => {
      wx.chooseLocation({
        success(res) {
          resolve(res);
        },
        fail(err){
          reject(err)
        }
      })
    })
  }

}
